package com.card.client.dialog;

import java.util.ArrayList;

import com.card.client.entry.FinanceType;
import com.card.client.entry.InputAccounts;
import com.card.client.entry.UserInfo;
import com.card.client.entry.Util;
import com.card.client.service.FinanceTypeConfigService;
import com.card.client.service.FinanceTypeConfigServiceAsync;
import com.card.client.service.InputAccountsService;
import com.card.client.service.InputAccountsServiceAsync;
import com.card.client.service.UserInfoManageService;
import com.card.client.service.UserInfoManageServiceAsync;
import com.card.client.view.InputAccountsView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;

public class EditInputAccountsDialog extends DialogBox {

	private static EditInputAccountsDialogUiBinder uiBinder = GWT
			.create(EditInputAccountsDialogUiBinder.class);

	interface EditInputAccountsDialogUiBinder extends UiBinder<Widget, EditInputAccountsDialog> {
	}

	private final FinanceTypeConfigServiceAsync ftcs = GWT.create(FinanceTypeConfigService.class);
	
	private final InputAccountsServiceAsync ias = GWT.create(InputAccountsService.class);
	
	private final UserInfoManageServiceAsync uims = GWT.create(UserInfoManageService.class);
	
	private Long id;
	
	@UiField TextBox explain;
	
	@UiField TextBox inputAccounts;
	
	@UiField ListBox financeType;
	
	@UiField DateBox appendTime;
	
	@UiField Button submit;
	
	@UiField Button closeDialog;
	
	@UiField Label message;
	
	InputAccountsView inputAccountsView;
	
	InputAccounts iAccounts;

	UserInfo loginedUser;
	
	public EditInputAccountsDialog(String title, String descript, InputAccountsView inputAccountsView, InputAccounts inputAccounts, int flag) {
		setWidget(uiBinder.createAndBindUi(this));
		setTitle(title);
		setText(descript);
		setGlassEnabled(true);
		setAnimationEnabled(true);
		setModal(true);
		message.setStyleName("descript");
		explain.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH * 3);
		this.inputAccounts.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		center();
		this.inputAccountsView = inputAccountsView;
		DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
		DefaultFormat defaultFormat = new DefaultFormat(dateTimeFormat);
		appendTime.setFormat(defaultFormat);
		submit.setText("添加");
		ftcs.getAllFinanceType(new AsyncCallback<FinanceType[]>() {
			public void onSuccess(FinanceType[] result) {
				ArrayList<FinanceType> list = new ArrayList<FinanceType>();
				for(int i=0;i<result.length;i++){
					FinanceType ft = result[i];
					if(ft.getTypeInOut() == Util.FINANCE_TYPE_AMOUNT_IN){
						list.add(ft);
					}
				}
				if(list.size()<1){
					financeType.addItem("没有收入财务类型，不能添加账目");
					submit.setEnabled(false);
				} else {
					financeType.addItem("", "0");
				}
				for(int i=0;i<list.size();i++){
					FinanceType ft = list.get(i);
					String id = Long.toString(ft.getId());
					if(ft.getTypeInOut() == Util.FINANCE_TYPE_AMOUNT_IN){
						financeType.addItem(ft.getFinanceType(), id);
					}
				}
				if(null != iAccounts){
					int index = 0;
					String id = Long.toString(iAccounts.getFinanceTypeId());
					for(int i=0;i<financeType.getItemCount();i++){
						String value = financeType.getValue(i);
						if(value.equals(id)){
							index = i;
						}
					}
					financeType.setSelectedIndex(index);
				}
			}
			public void onFailure(Throwable caught) {}
		});
		uims.getLoginedUser(new AsyncCallback<UserInfo>() {
			public void onSuccess(UserInfo result) {
				loginedUser = result;
			}
			public void onFailure(Throwable caught) {}
		});
		if(flag == Util.DIALOG_CONTROL_EDIT){
			submit.setText("修改");
			this.iAccounts = inputAccounts;
			setContent();
		}
		int w = this.inputAccounts.getOffsetWidth() + 1;
		financeType.setWidth(w + "px");
	}

	@UiHandler("submit")
	void onClickSubmit(ClickEvent e) {
		if(!isValid()){
			return;
		}
		InputAccounts ia = new InputAccounts();
		ia.setId(id);
		ia.setExplain(explain.getText());
		ia.setAccounts(Util.strToInt(inputAccounts.getText()));
		ia.setAppendTime(appendTime.getValue());
		int index = financeType.getSelectedIndex();
		String value = financeType.getValue(index);
		ia.setFinanceTypeId(Long.valueOf(value));
		if(null != id){
			ia.setUserId(iAccounts.getUserId());
			ias.updateInputAccounts(ia, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					inputAccountsView.onLoad();
				}
				public void onFailure(Throwable caught) {}
			});
		} else {
			ia.setUserId(loginedUser.getId());
			ias.addInputAccounts(ia, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					inputAccountsView.onLoad();
				}
				public void onFailure(Throwable caught) {}
			});
		}
	}

	@UiHandler("closeDialog")
	void onClickCloseDialog(ClickEvent e) {
		hide();
		inputAccountsView.onLoad();
	}
	
	public void setInputAccounts(InputAccounts inputAccounts){
		this.iAccounts = inputAccounts;
		setContent();
	}
	
	private void setContent(){
		if(iAccounts==null){
			return;
		}
		id = iAccounts.getId();
		int index = 0;
		for(int i=0;i<financeType.getItemCount();i++){
			String value = financeType.getItemText(i);
			if(value.equals(iAccounts.getFinanceTypeId())){
				index = i;
			}
		}
		financeType.setSelectedIndex(index);
		explain.setText(iAccounts.getExplain());
		inputAccounts.setText(Util.intToStr(iAccounts.getAccounts()));
		appendTime.setValue(iAccounts.getAppendTime());
	}
	
	private void setMessage(String msg){
		message.setText(msg);
	}
	
	private boolean isValid(){
		setMessage("");
		String check = Util.validInput(explain.getText());
		if(null != check){
			explain.setFocus(true);
			setMessage("收入说明" + check);
			return false;
		}
		check = Util.validInput(inputAccounts.getText());
		if(null != check){
			inputAccounts.setFocus(true);
			setMessage("收入金额" + check);
			return false;
		}
		check = Util.validAmount(inputAccounts.getText());
		if(null != check){
			inputAccounts.setFocus(true);
			setMessage(check);
			return false;
		}
		int index = financeType.getSelectedIndex();
		String value = financeType.getValue(index);
		if("0".equals(value)){
			financeType.setFocus(true);
			setMessage("请选择收入类型");
			return false;
		}
		check = Util.validInput(appendTime.getTextBox().getText());
		if(null != check){
			appendTime.setFocus(true);
			setMessage("收入日期" + check);
			return false;
		}
		return true;
	}
}
